var WebSocketServer = require('ws').Server
  ,restify= require('restify'),app= restify.createServer(),path= require('path');

app.use(restify.queryParser())
app.use(restify.CORS())
app.use(restify.fullResponse())
app.use(restify.gzipResponse())
app.use(restify.acceptParser(app.acceptable))
app.get('/', function (req, res, next){
  var data = require('fs').readFileSync(path.join(__dirname, 'index.html'));
  res.status(200);
  res.header('Content-Type', 'text/html');
  res.end(data.toString());
})
app.get(/^.*\.(html|png|svg|css|js|ico)$/,restify.serveStatic({directory: path.join(__dirname,'..','public')}));
app.listen(80)

var wss = new WebSocketServer({server: app});
wss.on('connection', function(ws) {
  var id = setInterval(function() {
    ws.send(JSON.stringify(process.memoryUsage()), function() { /* ignore errors */ });
  }, 100);
  console.log('started client interval');
  ws.on('close', function() {
    console.log('stopping client interval');
    clearInterval(id);
  });
});